﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SensorManager : MonoBehaviour
{
    // Transform knife;
    Knife knife;
    bool init = false;
    Vector3 target;

    public float xScale;
    public float xOffset;
    public float yScale;
    public float yOffset;
    public float zScale;
    public float zOffset;

    private void Awake()
    {
        int ret = SensorWrapperInterface.SensorInit();
        if (ret == 0) init = true;
        Debug.Log("Initialized sensor with return code " + ret.ToString());
    }

    // Start is called before the first frame update
    void Start()
    {
        // knife = GameObject.Find("knife").transform;
        // target = knife.position;
        knife = GetComponentInChildren<Knife>();
    }

    // Update is called once per frame
    void Update()
    {
        if (!init) return;
        SensorWrapperInterface.SensorUpdate();

        int x = 0, y = 0, z = 0;
        if (SensorWrapperInterface.SensorTryAccessData(ref x, ref y, ref z) == 0)
        {
            // Debug.Log(string.Format("{0}, {1}, {2}", x, y, z));
            if (knife == null) return;
            // target = new Vector3((-x+300) / 1000.0f, (-y+300) / 1000.0f, (z-400) / 1000.0f);
            knife.Target = new Vector3(x * xScale + xOffset, 
                        y * yScale + yOffset,
                        z * zScale + zOffset);
        } else
        {

        }
    }

    private void OnApplicationQuit()
    {
        if (!init) return;
        int ret = SensorWrapperInterface.SensorDestroy();
        Debug.Log("sensor destroyed with return code " + ret.ToString());
    }
}
